package com.dpf.hou.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dpf.hou.mapper.UserMapper;
import com.dpf.hou.pojo.User;
import com.dpf.hou.service.UserService;
import com.dpf.hou.util.Base64Util;
import com.dpf.hou.util.ResultUtil;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    @GetMapping("findUser")
    public boolean findUser(Integer userid){
        if(userMapper.selectUserid(userid)!=null){
            return false;
        }
        return true;
    }
    @GetMapping("getAllUser")
    public List<User> getAllUser(){
        return userService.getAllUser();
    }

    @GetMapping("login")
    public ResultUtil loginUser(String username,String password){
        String encodePassword = Base64Util.base64Encode(password);
        return userService.loginUser(username,encodePassword);
    }
    //增
    @PostMapping("addUser")
    public boolean addUser(User user){
        if(user.getPassword()!=null) {
            String encodePassword = Base64Util.base64Encode(user.getPassword());
            user.setPassword(encodePassword);
        }
        return userService.addUser(user);
    }
    //删
    @DeleteMapping("delUser")
    public boolean delUser(Integer userid){
        return userMapper.deleteById(userid);
    }
    @PutMapping("updateUser")
    public boolean updateUser(User user){
        System.out.println(user);
        return userService.updateUser(user);
    }

    @GetMapping("getPage")
    public Page<User> getPage(int page,int size,int[] userids){
        Page<User> res=userService.getPage(page,size,userids);
        return res;
    }

    @GetMapping("getPageByName")
    public Page<User> getPageByName(int page,int size,String username){
        Page<User> res=userService.getPageByName(page,size,username);
        return res;
    }

    @DeleteMapping("delBatch")
    public boolean delBatch(@RequestParam List<Integer> userid){
        boolean result=userService.delBatch(userid);

        return result;
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception{
        List<User> list=userService.list();
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("username","用户名");
        writer.addHeaderAlias("password","用户密码");
        writer.addHeaderAlias("email","邮箱");
        writer.addHeaderAlias("address","地址");
        writer.addHeaderAlias("intro","简介");
        writer.write(list,true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName= URLEncoder.encode("用户信息","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+ fileName +".xlsx");
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        out.close();
        writer.close();
    }

    @PostMapping("/fileImport")
    public boolean fileImport(MultipartFile file) throws Exception{
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<User> list = reader.readAll(User.class);
        System.out.println(list);
        userService.saveBatch(list);
        return true;
    }

    @PostMapping("upload")
    public ResultUtil upload(MultipartFile uploadFile) {
//声明上传地址
        String path = "d://upload";
//判断上传地址是否存在，如果不存在，要创建
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
//获取上传的文件名
        String fileName = UUID.randomUUID()+uploadFile.getOriginalFilename();
//创建新的文件对象
        File newFile = new File(file, fileName);
//将新的文件对象放到指定位置
        try {
            uploadFile.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ResultUtil resultUtil = null;
//返回给前端当前上传的文件在文件服务器上的地址
        String url = "http://localhost:8888/upload/"+fileName;
//返回给前端当前上传的是图片还是视频
        if (fileName.endsWith("jpg") || fileName.endsWith("png") ||
                fileName.endsWith("gif") || fileName.endsWith("jpeg")) {
//是图片
            resultUtil = new ResultUtil(1,"图片上传成功",url);
        } else {
//是视频
            resultUtil = new ResultUtil(2,"视频上传成功",url);
        }
        return resultUtil;
    }

    @PutMapping("pwdChange")
    public boolean pwdChange(User user){
        return userService.pwdChange(user);
    }


    @GetMapping("getEncode")
    public String getEncode(String password){
        return userService.getEncode(password);
    }


}
